#include<QTextStream>
#include<iostream>
#include<QString>

using namespace std;

QString getLongestCommonPrefix(QList<QString> strs)
{
    // 输入的字符串个数size需硬编码
    int size = 4, count, minLength;

    // 通过for循环寻找最短字符串的长
    minLength =  strs[0].size();
    for(int i = 0; i < size - 1; i++)
    {
        if(strs[i].size() > strs[i+1].size())
        {
            minLength =  strs[i+1].size();
        }
    }

    // 声明并初始化共同前缀
    QString common = "";

    // 判断每一位字符是否属于共同前缀
    for(int i = 0, j = 0; i < minLength; i++)
    {
        count = 0;

        for(int k = 0; k < size - 1; k++)
        {
            if(strs[k][i] == strs[k+1][i])
            {
                count++;
            }
            if(count == size - 1 && i == j)  // 若每个字符串的这一位都相同那么把它记录在common中
            {
                common += strs[k][i];
                j++;
            }
        }
    }

    return common;
}

int main()
{
    QList<QString> strs;
    strs << "abcde" << "ab" << "abc"<<"abcd";  // 硬编码,输入size个字符串,此处的个数需要与函数中size相当

    // 声明共同前缀
    QString common;
    common = getLongestCommonPrefix(strs);    //通过函数将最长公共前缀赋予common
    cout << "the longest commonprefix is:" << common.toStdString().data() << endl;

    return 0;
}
